package com.stylefeng.guns.modular.system.controller;

import com.stylefeng.guns.common.annotion.Permission;
import com.stylefeng.guns.common.annotion.log.BussinessLog;
import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.constant.Dict;
import com.stylefeng.guns.common.constant.factory.ConstantFactory;
import com.stylefeng.guns.common.constant.state.ManagerStatus;
import com.stylefeng.guns.common.constant.tips.Tip;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.dao.UserMapper;
import com.stylefeng.guns.common.persistence.model.OpUser;
import com.stylefeng.guns.common.persistence.model.User;
import com.stylefeng.guns.config.properties.GunsProperties;
import com.stylefeng.guns.core.db.Db;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.OpUserDao;
import com.stylefeng.guns.modular.system.dao.UserMgrDao;
import com.stylefeng.guns.modular.system.factory.UserFactory;
import com.stylefeng.guns.modular.system.transfer.UserDto;
import com.stylefeng.guns.modular.system.warpper.OpUserWarpper;
import com.stylefeng.guns.modular.system.warpper.UserWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.naming.NoPermissionException;
import javax.validation.Valid;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 系统网站用户控制器
 *
 * @author fengshuonan
 * @Date 2017年1月11日 下午1:08:17
 */
@Controller
@RequestMapping("/opuser")
public class OpUserController extends BaseController {

    private static String PREFIX = "/system/opuser/";

    @Resource
    private GunsProperties gunsProperties;

    @Resource
    private OpUserDao opUserDao;

    /**
     * 跳转到查看网站用户列表的页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "opuser.html";
    }

    /**
     * 跳转到查看网站用户列表的页面
     */
    @RequestMapping("/opuser_add")
    public String addView() {
        return PREFIX + "opuser_add.html";
    }

    /**
     * 跳转到编辑网站用户页面
     */
    @RequestMapping("/opuser_edit/{id}")
    public String userEdit(@PathVariable Integer id, Model model) {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        OpUser opuser = this.opUserDao.selectById(id);
        model.addAttribute("opuser",opuser);
        LogObjectHolder.me().set(opuser);
        return PREFIX + "opuser_edit.html";
    }

    /**
     * 查询网站用户列表
     */
    @RequestMapping("/list")
    @Permission
    @ResponseBody
    public Object list(@RequestParam(required = false) String user_name, @RequestParam(required = false) String beginTime, @RequestParam(required = false) String endTime) {
        List<Map<String, Object>> opusers = opUserDao.selectOpUsers(user_name, beginTime, endTime);
        return new OpUserWarpper(opusers).warp();
    }

    /**
     * 添加网站用户
     */
    @RequestMapping("/add")
    @BussinessLog(value = "添加网站用户", key = "account", dict = Dict.UserDict)
    @ResponseBody
    public Object add(OpUser opuser) {
        // 判断账号是否重复
        OpUser theUser = opUserDao.getByUserName(opuser.getUser_name());
        if (theUser != null) {
            throw new BussinessException(BizExceptionEnum.USER_ALREADY_REG);
        }
        opuser.setJoin_date(new Date());
        this.opUserDao.insert(opuser);
        return SUCCESS_TIP;
    }

    /**
     * 修改网站用户
     *
     * @throws NoPermissionException
     */
    @RequestMapping("/edit")
    @BussinessLog(value = "修改网站用户", key = "account", dict = Dict.UserDict)
    @ResponseBody
    public Object edit(OpUser opuser) {
        // 判断账号是否重复
        int num = opUserDao.getCountByUserName(opuser.getId(),opuser.getUser_name());
        if (num > 0) {
            throw new BussinessException(BizExceptionEnum.USER_ALREADY_REG);
        }
        opUserDao.updateById(opuser);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除网站用户（逻辑删除）
     */
    @RequestMapping("/delete")
    @BussinessLog(value = "删除网站用户", key = "userId", dict = Dict.UserDict)
    @ResponseBody
    public Object delete(@RequestParam Integer id) {
        //缓存被删除的用户名称
        LogObjectHolder.me().set(opUserDao.getOpUserNameById(id));

        opUserDao.deleteById(id);

        return SUCCESS_TIP;
    }
}
